package com.amc.common.manager;

import com.amc.common.utils.spring.SpringUtils;
import org.apache.skywalking.apm.toolkit.trace.CallableWrapper;
import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 异步执行管理器
 * @author ZhangXX
 * @date @2021/9/7 10:56
 */
public class AsyncManager {

    /**
     * 异步操作任务调度线程池
     */
    private ThreadPoolTaskExecutor executor = SpringUtils.getBean("threadPoolTaskExecutor");

    /**
     * 单例模式
     */
    private AsyncManager(){}

    private static AsyncManager me = new AsyncManager();

    public static AsyncManager me()
    {
        return me;
    }

    /**
     * 执行任务
     *
     * @param task 任务
     */
    public void execute(TimerTask task)
    {
        executor.submit(task);
    }

    /**
     * 执行任务
     *
     * @param task 任务
     */
    public void execute(Runnable task)
    {
        executor.submit(RunnableWrapper.of(task));
    }
    /**
     * 执行任务
     *
     * @param task 任务
     */
    public Future<?> execute(Callable task) throws ExecutionException, InterruptedException {
        return executor.submit(CallableWrapper.of(task));
    }

}
